<include file="public:header" />
<script charset="utf-8" src="__STATIC__/weixin/js/jquery.js" type="text/javascript"></script>
<script>
//增加的颜色
var colorAdd = 0;
//增加的型号
var sizeAdd = 0;
//增加一个型号时颜色的个数
var colorlength = 0;
//增加一个颜色时型号的个数
var sizelength = 0;
//增加颜色时细则库存对应的型号计数器
var size_stock=0;
//增加型号时细则库存对应的颜色计数器
var color_stock=0;
function addSize()
{
	sizeAdd = sizeAdd + 1;
  var colors = document.getElementsByName("color[]");
  colorlength=colors.length;
  for(i=0;i<colors.length;i++){
  	color_stock=color_stock+1;
  	$str='';
      $str+="<tr align='center' style='border-left:5px solid #8B4513;' id='trcolor"+color_stock+"' class='stock_tr' title='"+colors[i].value+"|'>";
      
      $str+="<td>"+colors[i].value+"</td>";
      $str+="<td><div name='ouptsize"+sizeAdd+"' class='ouptsize"+sizeAdd+"'></div></td>";
      $str+="<td><input type='number' onchange='sum_stock()' value='0' id='stockcolor"+color_stock+"' title='"+colors[i].value+"|' name='stockcolor' /></td>";
      $str+="</tr>";
      $("#addTr").append($str);   

  }
  $("#addsize_action").attr("disabled",true);
  $("#addcolor_action").attr("disabled",true);
  var sizeid=$("#size");
  sizeid.append("<input  onchange='keydown_size()' name='size[]' type='text' id='mysize" + sizeAdd + "' size='12'><img id='inpu_img" + sizeAdd + "' onclick='delete_sizetr(" + sizeAdd + ")' height='15' width='15' style='margin-right:3px;' src='/tpl/User/default/common/images/icons/delete.png' />");
}
function addColor()
{
	colorAdd = colorAdd + 1;
  var sizeid=$("#color");
  var sizes = document.getElementsByName("size[]");
  sizelength = sizes.length;
  for(i=0;i<sizes.length;i++){
  	size_stock = size_stock + 1;
  	$str='';
      $str+="<tr align='center' style='border-left:5px solid #8968CD;' id='trsize"+size_stock+"' title='|"+sizes[i].value+"' class='stock_tr'>";
      $str+="<td><div name='oupt"+colorAdd+"' class='oupt"+colorAdd+"'></div></td>";
      $str+="<td>"+sizes[i].value+"</td>";
      $str+="<td><input onchange='sum_stock()' type='number' id='stock"+size_stock+"' title='|"+sizes[i].value+"' value='0' name='stocksize' /></td>";
      $str+="</tr>";
      $("#addTr").append($str);   

  }
  $("#addsize_action").attr("disabled",true);
  $("#addcolor_action").attr("disabled",true);
  sizeid.append("<input onchange='keydown()' name='color[]' id='mycolor" + colorAdd + "' type='text' size='12'><img id='color_img" + colorAdd + "' onclick='delete_colortr(" + colorAdd + ")' height='15' width='15'  style='margin-right:3px;' src='/tpl/User/default/common/images/icons/delete.png' />");
}
/**
* 删除型号，删除相应的其他相关的
*/
function delete_sizetr(sizename){
	var stocktr=$(".stock_tr");
	var sizestr="";
	for(i=0;i<stocktr.length;i++){
		sizestr=stocktr[i].title.split("|");
		if(sizestr[1] == sizename){
			stocktr[i].remove();
		}else if(sizestr[1] == ""){
			stocktr[i].remove();
		}
	}
	if($("#mysize"+sizename).val() == ""){
		$("#addsize_action").attr("disabled",false);
	    $("#addcolor_action").attr("disabled",false);
	}

	$("#mysize"+sizename).remove();
	$("#inpu_img"+sizename).remove();
}
/**
* 删除颜色，删除相应的其他相关的
*/
function delete_colortr(colorname){
	var stocktr=$(".stock_tr");
	var sizestr="";
	for(i=0;i<stocktr.length;i++){
		sizestr=stocktr[i].title.split("|");
		if(sizestr[0] == colorname){
			stocktr[i].remove();
		}else if(sizestr[0] == ""){
			stocktr[i].remove();
		}
	}
	
	$("#mycolor"+colorname).remove();
	if($("#mycolor"+colorname).val() == ""){
		$("#addsize_action").attr("disabled",false);
	    $("#addcolor_action").attr("disabled",false);
	}
	$("#color_img"+colorname).remove();
}
function keydown(){
	//输入新颜色的时候改变新增库存行的 title
	var min_stock = size_stock - sizelength;
	var new_title = "";
	var new_title_color = "";
	for(i=size_stock;i>min_stock;i--){
		new_title=$("#mycolor"+colorAdd).val() + document.getElementById("stock"+i).title;
		new_title_color=colorAdd + document.getElementById("stock"+i).title;
		$("#trsize"+i).attr("title",new_title_color);
		 $("#stock"+i).attr("title",new_title);
		// alert(new_title);
	 }
	if($("#mycolor"+colorAdd).val() == ""){
		$("#addsize_action").attr("disabled",true);
	    $("#addcolor_action").attr("disabled",true);
	}else{
		$("#addsize_action").attr("disabled",false);
	    $("#addcolor_action").attr("disabled",false);
	}
	$(".oupt"+colorAdd).html($("#mycolor"+colorAdd).val());
	}
function keydown_size(){
	//输入新型号的时候改变新增库存行的 title
	var min_stock = color_stock - colorlength;
	var new_title = "";
	var new_title_size="";
	for(i=color_stock;i>min_stock;i--){
		new_title=document.getElementById("stockcolor"+i).title + $("#mysize"+sizeAdd).val();
		new_title_size=document.getElementById("stockcolor"+i).title + sizeAdd;
		 $("#stockcolor"+i).attr("title",new_title); 
		 $("#trcolor"+i).attr("title",new_title_size);
		 //alert(new_title);
	 }
	if($("#mysize"+sizeAdd).val() == ""){
		$("#addsize_action").attr("disabled",true);
	    $("#addcolor_action").attr("disabled",true);
	}else{
		$("#addsize_action").attr("disabled",false);
	    $("#addcolor_action").attr("disabled",false);
	}
	$(".ouptsize"+sizeAdd).html($("#mysize"+sizeAdd).val());
	}
	
	
/*
**下面的方法主要是把细则库存通过拼接字符串传入数据库
**字符串的格式是  ‘颜色|型号|库存’
**/
function deal_stock(){
	
	var stock_detail = "";
	 var sizescolor = document.getElementsByName("stockcolor");
	 for(i=0;i<sizescolor.length;i++){
		 if(sizescolor[i].value !=""){
			 stock_detail = stock_detail + sizescolor[i].title + "|" + sizescolor[i].value + ",";
		 }
	 }
	 var sizessize = document.getElementsByName("stocksize");
	 for(i=0;i<sizessize.length;i++){
		 if(sizessize[i].value !=""){
			 stock_detail = stock_detail + sizessize[i].title + "|" + sizessize[i].value + ",";
		 }
	 }
	 $("#detail_stock").val(stock_detail);
	 $("#info_form").submit();
}

/*
**下面的方法当细则库存任意一个改变时 总库存重新计算一下
**/
function sum_stock(){
	 var sum_num = 0;
	 
	 var sizescolor = document.getElementsByName("stockcolor");
	 for(i=0;i<sizescolor.length;i++){
		 if(sizescolor[i].value !=""){
		     sum_num = sum_num + parseInt(sizescolor[i].value);
		 }
	 }
	 var sizessize = document.getElementsByName("stocksize");
	 for(i=0;i<sizessize.length;i++){
		 if(sizessize[i].value !=""){
		     sum_num = sum_num + parseInt(sizessize[i].value);
		 }
	 }
	$("#J_goods_stock").val(sum_num);
}

	$(function(){
    	$('#free').change(function(){
        	if($(this).val()==2)
        	{
        	  	$('#address_form').show();
        	}
        	else
        	{
        	  	$('#address_form').hide();
        	}
        });
        set_address();
    })
        
    function set_address()
    {
        var addr_id =$("#free").find("option:selected").val();
        if(addr_id == 2)
        {
            $('#address_form').show();
        }
        else
        {
            $('#address_form').hide();
        }
    }
	function addSize()
    {
        var sizeid=$("#size");
        sizeid.append("<input name='size[]' type='text' size='12'>&nbsp;");
    }
	function addColor()
    {
        var colorid=$("#color");
        colorid.append("<input name='color[]' type='text' size='12'>&nbsp;");
    }
</script>
<!--添加商品-->
<div class="subnav">
    <h1 class="title_2 line_x">添加商品</h1>
</div>
<form id="info_form" action="{:U('item/add',array('tokenTall'=>$tokenTall))}" method="post" enctype="multipart/form-data">
<div class="pad_lr_10">
	<div class="col_tab">
		<ul class="J_tabs tab_but cu_li">
			<li class="current">基本信息</li>
            <li>展示图片</li>
		</ul>
		<div class="J_panes">
        <div class="content_list pad_10">
		<table width="100%" cellpadding="2" cellspacing="1" class="table_form">
			<tr>
				<th width="120">所属分类 :</th>
                <td><select class="J_cate_select mr10" data-pid="0" data-uri="{:U('item_cate/ajax_getchilds', array('type'=>0))}" data-selected=""></select><input type="hidden" name="cate_id" id="J_cate_id" value="" /></td>
			</tr>
			<tr>
				<th width="120">型号 :</th>
                <td> <div id="size"><input type="button" id="addsize_action" value="  添加    " onclick="addSize()">
                     
                     </div>
                    </td>
			</tr>
			<tr>
				<th width="120">颜色 :</th>
                <td><div id="color"><input type="button" id="addcolor_action" value="  添加    " onclick="addColor()">
                     
                     </div></td>
			</tr>
			<tr>
			    <th width="120">库存细则：</th>
			    <td>
			    <table id="addTr">
			    </table>
			    <input type="hidden" id="detail_stock" name="detail_stock">
			    </td>			
			</tr> 
			<tr>
				<th width="120">所属品牌 :</th>
                <td>
                <select  name="brand" >
                <option value="">--请选择--</option>
                <volist name='brandlist' id='vo' >
                <option value="{$vo.id}" >{$vo.name}</option>
                </volist>
                </select></td>
			</tr>
            <tr>
				<th>商品名称 :</th>
				<td><input type="text" name="title" id="J_title" class="input-text" size="60"></td>
			</tr>			
			<tr>
                <th>商品简介 :</th>
                <td><textarea name="intro" cols="80" rows="2"></textarea></td>
            </tr>
            <tr>
				<th>商品图片 :</th>
				<td><input type="file" name="img" /></td>
 			</tr>
		
            <tr>
				<th>商品价格 :</th>
				<td><input id='J_price' onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" onafterpaste="this.value=this.value.replace(/[^0-9.]/g,'')" type="text" name="price" class="input-text" size="10"> 元</td>
			</tr>
		    <tr>
				<th>商品库存 :</th>
				<td><input id='J_goods_stock' onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" type="text" name="goods_stock" class="input-text" size="10"> </td>
			</tr>
		    <tr>
		      <th>是否推荐:</th>
		      <td><input type="checkbox" name="news" >新品&nbsp; <input type="checkbox" name="tuijian" >推荐</td>
		    </tr>
		     <tr>
		      <th>运费:</th>
		      <td>
		      <select id='free' name="free">
		      <option value="1">卖家承担运费</option>
		      <option  value="2">买家承担运费</option>
		      </select>
		      </td>
		    </tr>
		    <tr id="address_form" style="display:none;">
		    <th></th>
		    <td> 
		           平邮:<input onkeyup="value=value.replace(/[^\d\.]/g,'')" type="text" name="pingyou" />
		           快递:<input onkeyup="value=value.replace(/[^\d\.]/g,'')" type="text" name="kuaidi" />
		    EMS:<input onkeyup="value=value.replace(/[^\d\.]/g,'')" type="text" name="ems" /></td>
		    </tr>
		    <tr>
				<th>商品详情 :</th>
		        <td><textarea name="info" id="info" style="width:68%;height:400px;visibility:hidden;resize:none;"></textarea></td>
			</tr>
		</table>
		</div>
        <div class="content_list pad_10 hidden">
            <table width="100%" cellpadding="2" cellspacing="1" class="table_form" id="first_upload_file">
                <tbody class="uplode_file">
                <tr>
                    <th width="100"><a href="javascript:void(0);" class="blue" onclick="add_file();"><img src="__STATIC__/css/admin/bgimg/tv-expandable.gif" /></a> 上传文件 :</th>
                    <td><input type="file" name="imgs[]"></td>
                </tr>
                </tbody>
            </table>
        </div>
		<div class="content_list pad_10 hidden">
		<table width="100%" cellpadding="2" cellspacing="1" class="table_form">
			<tr>
				<th width="120">{:L('seo_title')} :</th>
 				<td><input type="text" name="seo_title" id="seo_title" class="input-text" size="60"></td>
			</tr>
			<tr>
				<th>{:L('seo_keys')} :</th>
				<td><input type="text" name="seo_keys" id="seo_keys" class="input-text" size="60"></td>
			</tr>
			<tr>
				<th>{:L('seo_desc')} :</th>
				<td><textarea name="seo_desc" id="seo_desc" cols="80" rows="8"></textarea></td>
			</tr>
		</table>
		</div>
        <div class="content_list pad_10 hidden">
		<table width="100%" cellpadding="2" cellspacing="1" class="table_form" id="item_attr">
			<tbody class="add_item_attr">
            <tr>
                <th width="210">
                <a href="javascript:void(0);" class="blue" onclick="add_attr();"><img src="__STATIC__/css/admin/bgimg/tv-expandable.gif" /></a> 属性名 :<input type="text" name="attr[name][]" class="input-text" size="20">
                </th>
                <td> 属性值 :<input type="text" name="attr[value][]" class="input-text" size="30"></td>
            </tr>
            </tbody>
		</table>
		</div>
        </div>
		<div class="mt10"><input type="submit" value="{:L('submit')}" class="btn btn_submit"></div>
	</div>
</div>
<input type="hidden" name="menuid"  value="{$menuid}"/>
</form>

<include file="public:footer" />
<script type="text/javascript">
$('.J_cate_select').cate_select('请选择');
$(function() {
	$('ul.J_tabs').tabs('div.J_panes > div');
	//自动获取标签
	$('#J_gettags').live('click', function() {
		var title = $.trim($('#J_title').val());
		if(title == ''){
			$.pinphp.tip({content:lang.article_title_isempty, icon:'alert'});
			return false;
		}
		$.getJSON('{:U("item/ajax_gettags")}', {title:title}, function(result){
			if(result.status == 1){
				$('#J_tags').val(result.data);
			}else{
				$.pinphp.tip({content:result.msg});
			}
		});
	});
	$.formValidator.initConfig({formid:"info_form",autotip:true});
	$("#J_title").formValidator({onshow:'请填写商品名称',onfocus:'请填写商品名称'}).inputValidator({min:1,onerror:'请填写商品名称'});
	$("#J_price").formValidator({onshow:'请填写商品价格',onfocus:'请填写商品价格'}).inputValidator({min:1,onerror:'请填写商品价格'});
	$("#J_goods_stock").formValidator({onshow:'请填写商品库存',onfocus:'请填写商品库存'}).inputValidator({min:1,onerror:'请填写商品库存'});
});

function add_file()
{
    $("#next_upload_file .uplode_file").clone().insertAfter($("#first_upload_file .uplode_file:last"));
}
function del_file_box(obj)
{
	$(obj).parent().parent().remove();
}
function add_attr()
{
    $("#hidden_attr .add_item_attr").clone().insertAfter($("#item_attr .add_item_attr:last"));
}
function del_attr(obj)
{
	$(obj).parent().parent().remove();
}
</script>
<table id="next_upload_file" style="display:none;">
<tbody class="uplode_file">
   <tr>
      <th width="100"><a href="javascript:void(0);" onclick="del_file_box(this);" class="blue"><img src="__STATIC__/css/admin/bgimg/tv-collapsable.gif" /></a>上传文件 :</th>
      <td><input type="file" name="imgs[]"></td>
   </tr>
</tbody>
</table>
<table id="hidden_attr" style="display:none;">
<tbody class="add_item_attr">
<tr>
    <th width="200">
    <a href="javascript:void(0);" class="blue" onclick="del_attr(this);"><img src="__STATIC__/css/admin/bgimg/tv-collapsable.gif" /></a>属性名 :<input type="text" name="attr[name][]" class="input-text" size="20">
    </th>
    <td>属性值 :<input type="text" name="attr[value][]" class="input-text" size="30"></td>
</tr>
</tbody>
</table>
</body>
</html>
<script src="__STATIC__/js/jquery/plugins/colorpicker.js"></script>
<script src="__STATIC__/js/kindeditor/kindeditor.js"></script>	
<script>

$(function() {
	KindEditor.create('#info', {
		uploadJson : '{:U("attachment/editer_upload")}',
		fileManagerJson : '{:U("attachment/editer_manager")}',
		allowFileManager : true
	});
	$('ul.J_tabs').tabs('div.J_panes > div');

	//颜色选择器
	$('.J_color_picker').colorpicker();

	//自动获取标签
	$('#J_gettags').live('click', function() {
		var title = $.trim($('#J_title').val());
		if(title == ''){
			$.pinphp.tip({content:lang.article_title_isempty, icon:'alert'});
			return false;
		}
		$.getJSON('{:U("article/ajax_gettags")}', {title:title}, function(result){
			if(result.status == 1){
				$('#J_tags').val(result.data);
			}else{
				$.pinphp.tip({content:result.msg});
			}
		});
	});
	
});
</script><br>